package top.jolyoulu.webcommon.filter;


import org.apache.catalina.util.ParameterMap;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * @Author: JolyouLu
 * @Date: 2023/1/21 16:17
 * @Description 构建可重复读取的Request
 */
public class MultiReadHttpServletRequestWrapper extends HttpServletRequestWrapper {

    //body数据
    private byte[] bytes;
    //parameter数据
    private ParameterMap<String, String[]> params;

    public MultiReadHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        try (ServletInputStream inputStream = request.getInputStream()) {
            bytes = new byte[request.getContentLength()];
            inputStream.read(bytes);
        }
        params = (ParameterMap) request.getParameterMap();
    }


    @Override
    public BufferedReader getReader() {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    /**
     * 重写getInputStream方法
     * 返回一个可以重复读的 ServletInputStream
     *
     * @return
     */
    @Override
    public ServletInputStream getInputStream() {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        return new ServletInputStream() {
            @Override
            public int read() {
                return byteArrayInputStream.read();
            }

            @Override
            public int available() {
                return bytes.length;
            }

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }
        };
    }

    /**
     * 获取参数
     */
    @Override
    public String getParameter(String name) {
        String[] values = params.get(name);
        if (values == null || values.length == 0) {
            return null;
        }
        return values[0];
    }

    /**
     * 获取参数Map
     */
    @Override
    public Map<String, String[]> getParameterMap() {
        return params;
    }

    /**
     * 添加参数
     */
    public void addParameter(String name, Object value) {
        if (value != null) {
            params.setLocked(false);
            if (value instanceof String[]) {
                params.put(name, (String[]) value);
            } else if (value instanceof String) {
                params.put(name, new String[]{(String) value});
            } else {
                params.put(name, new String[]{String.valueOf(value)});
            }
            params.setLocked(true);
        }
    }

    /**
     * 删除参数
     */
    public void delParameter(String name) {
        params.setLocked(false);
        params.remove(name);
        params.setLocked(true);
    }

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }
}
